<template>
  <div class="WorkRecord">
    <NavBar
      fixed
      placeholder
      title="作业记录"
      z-index="999"
      @click-right="onClickRight"
      ref="nav"
    >
      <template #left>
        <Icon
          name="arrow-left"
          size="0.53rem"
          color="#000"
          @click="$router.back()"
        />
      </template>
      <template #right>
        <Icon
          name="filter-o"
          size="0.53rem"
          color="#000"
          @click="show = true"
        />
      </template>
    </NavBar>
    <Search
      v-model="search"
      background="#e1e1e1"
      placeholder="请输入搜索关键词"
      show-action
      @search="onSearch1"
      @cancel="onCancel"
    />
    <PullRefresh v-model="isLoading" @refresh="onRefresh">
      <List
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onSearch"
      >
        <div
          class="item"
          v-for="(item, i) in list"
          :key="i"
          @click="toJump(item)"
        >
          <div class="row">
            <div class="title flex">
              作业编号
              <div class="id">
                {{ item.dgwkId }}
              </div>
              <Icon name="arrow" class="ico" />
            </div>
            <div class="state">{{ item.dgwkState }}</div>
          </div>
          <div class="row row1">
            <div class="placeholder"></div>
            <div class="type">{{ item.title }}</div>
          </div>
          <div class="main">
            <div class="row-item">
              <div class="attr">作业位置</div>
              <div class="val">{{ item.dgwkWorkPosition }}</div>
            </div>
            <div class="row-item" v-if="'level' in item">
              <div class="attr">作业级别</div>
              <div class="val">{{ item.level }}</div>
            </div>
            <div class="row-item">
              <div class="attr">申请时间</div>
              <div class="val">{{ item.firstDtm }}</div>
            </div>
            <img
              v-if="item.dfwExamineState == 2"
              src="../../../assets/img/gaizhang/shenhegaizhang2.png"
            />
            <img
              v-if="item.dfwExamineState == 3"
              src="../../../assets/img/gaizhang/shenhegaizhang3.png"
            />
            <img
              v-if="item.dfwExamineState == 4"
              src="../../../assets/img/gaizhang/shenhegaizhang4.png"
            />
            <img
              v-if="item.dfwExamineState == 5"
              src="../../../assets/img/gaizhang/shenhegaizhang5.png"
            />
            <img
              v-if="item.dfwExamineState == 6"
              src="../../../assets/img/gaizhang/shenhegaizhang6.png"
            />
            <img
              v-if="item.dfwExamineState == 7"
              src="../../../assets/img/gaizhang/shenhegaizhang7.png"
            />
            <img
              v-if="item.dfwExamineState == 8"
              src="../../../assets/img/gaizhang/shenhegaizhang8.png"
            />
            <img
              v-if="item.dfwExamineState == 9"
              src="../../../assets/img/gaizhang/shenhegaizhang9.png"
            />
          </div>

          <!-- <div class="main">211231</div> -->
          <div class="row3" v-if="item.oprName">
            <!-- <div class="row3"> -->
            <Button
              round
              color="#E16712"
              plain
              @click.stop="prompt(item)"
              size="small"
              >{{ item.oprName }}</Button
            >
            <!-- <Button class="btn btn1" size="small" @click.stop="show2 = true"
            >延期</Button
          >
          <Button class="btn" size="small" @click.stop="show3 = true"
            >终结</Button
          > -->
          </div>
        </div>
      </List>
    </PullRefresh>

    <Popup
      class="prompt"
      v-model="show1"
      :style="{ height: '300px', width: '70%' }"
    >
      <div class="title">此操作将确认{{ postText }}</div>
      <div class="main">
        <!-- <div class="message">
          <Icon name="warning" class="tag" size="0.53rem" color="#f78a0d" />
          此操作将确认{{ postText }}，是否继续？
        </div> -->
        <div v-if="postText == '现场审核通过'">
          <Field label="现场布置照片" :border="false" readonly></Field>
          <div class="uploaderList">
            <Uploader
              v-model="sitePhoto"
              multiple
              :max-count="3"
              capture="camera"
            />
          </div>
        </div>
        <div v-if="postText == '验收通过'">
          <Field label="验收照片" :border="false" readonly></Field>
          <div class="uploaderList">
            <Uploader
              v-model="accePic"
              multiple
              :max-count="3"
              capture="camera"
            />
          </div>
        </div>

        <div class="btns">
          <Button class="btn" size="small" @click="show1 = false">取消</Button>
          <Button
            class="btn"
            type="info"
            color="#E16712"
            size="small"
            @click="confirm"
            >确认</Button
          >
        </div>
      </div>
    </Popup>

    <!-- 菜单 -->
    <Popup
      class="Popup"
      v-model="show"
      position="right"
      :style="{ height: '100%', width: '70%' }"
      @closed="closed"
    >
      <div class="task">
        <div class="title">作业状态</div>
        <div class="state">
          <div
            class="state-item flex"
            v-for="(item, i) in stateList"
            :key="i"
            @click="changeState(item)"
          >
            <!-- item.state = !item.state -->
            <div class="btn3 flex" :class="{ change: item.state == true }">
              {{ item.name }}
            </div>
          </div>
        </div>
        <div class="title">审核状态</div>
        <div class="state">
          <div
            class="state-item flex"
            v-for="(item, i) in stateList1"
            :key="item.id"
            @click="changeState1(item)"
          >
            <div class="btn3 flex" :class="{ change: item.state == true }">
              {{ item.name }}
            </div>
          </div>
        </div>
        <div class="title">作业级别</div>
        <div class="state">
          <div
            class="state-item flex"
            v-for="(item, i) in lvList"
            :key="i"
            @click="item.state = !item.state"
          >
            <div class="btn3 flex" :class="{ change: item.state == true }">
              {{ item.name }}
            </div>
          </div>
        </div>
      </div>

      <div class="title">申请时间</div>
      <div class="flex">
        <input
          type="text"
          placeholder="请选择开始时间"
          class="ipt1"
          v-model="startTime"
          readonly
          @click="showTime1 = true"
        />
        <span class="placeholder1 flex">一</span>
        <input
          type="text"
          placeholder="请选择结束时间"
          class="ipt1"
          v-model="endTime"
          readonly
          @click="showTime2 = true"
        />
      </div>
      <div class="btn-box flex">
        <Button class="reset" @click="reset">重置</Button>
        <Button class="ok" @click="ok">确定</Button>
      </div>
    </Popup>

    <Popup
      class="time"
      v-model="showTime1"
      position="bottom"
      :style="{ height: '50%' }"
    >
      <DatetimePicker
        v-model="currentDate"
        type="datetime"
        title="作业开始时间"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="startConfirm"
        @cancel="showTime1 = false"
        item-height="1rem"
      ></DatetimePicker>
    </Popup>
    <Popup
      class="time"
      v-model="showTime2"
      position="bottom"
      :style="{ height: '50%' }"
    >
      <DatetimePicker
        v-model="currentDate"
        type="datetime"
        title="作业结束时间"
        :min-date="minDate1"
        :max-date="maxDate1"
        @confirm="endConfirm"
        @cancel="showTime2 = false"
        item-height="1rem"
      ></DatetimePicker>
    </Popup>
  </div>
</template>

<script>
import uploadImages from "@/utils/uploadImages.js";
import {
  WhenHotList,
  WhenScreeningList,
  InitiateTheProcess,
} from "../../../api/work.js";
import {
  Tag,
  Dialog,
  Button,
  Search,
  NavBar,
  Icon,
  Cell,
  List,
  Popup,
  DatetimePicker,
  PullRefresh,
  Uploader,
  Field,
} from "vant";
export default {
  props: {},
  data() {
    return {
      search: "",
      list: [
        // {
        //   title: "123456",
        //   state: "待布置",
        //   type: "电梯",
        //   location: "A区木有鱼客栈",
        //   lv: "一级",
        //   date: "2021-02-02",
        //   time: "12:20:36",
        // },
        // {
        //   title: "32154645",
        //   state: "作业中",
        //   type: "电梯",
        //   location: "A区木有鱼客栈",
        //   lv: "一级",
        //   date: "2021-02-02",
        //   time: "12:20:36",
        // },
      ],
      loading: false,
      finished: false,
      show: false,
      stateList: [],
      stateList1: [],
      stateListClone: [],
      stateListClone1: [],
      lvList: [],
      user: "",
      phone: "",
      startTime: "",
      endTime: "",
      show1: false,
      show2: false,
      show3: false,
      showTime1: false,
      showTime2: false,
      minDate: new Date(2020, 0, 1),
      minDate1: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      maxDate1: new Date(2025, 10, 1),
      currentDate: new Date(2021, 0, 17),
      pwd: "",
      icoNo: "",
      pageNo: 1,
      pageSize: 10,
      postItem: {},
      postText: "",
      isLoading: false,
      fileList: [],
      sitePhoto: [],
      accePic: [],
    };
  },
  computed: {
    level() {
      //作业级别数据处理
      let dfwLevel = this.lvList.filter((v) => v.state);
      if (dfwLevel.length > 0) {
        let arr = [];
        dfwLevel.forEach((v) => {
          arr.push(v.id);
        });
        return arr.join(",");
      }
      return "";
    },
    stateListId() {
      // 作业状态
      let arr = [];
      let dfwStatus = this.stateList.filter((v) => v.state);
      dfwStatus.forEach((v) => {
        arr.push(v.id);
      });
      return arr.join(",");
    },
    stateListId1() {
      // 审核状态
      let dfwExamineStatus = this.stateList1.filter((v) => v.state);
      let arr = [];
      dfwExamineStatus.forEach((v) => {
        arr.push(v.id);
      });
      return arr.join(",");
    },
  },
  created() {},
  mounted() {
    this.getMenuList();
    this.icoNo = this.$route.query.icoNo;
  },

  methods: {
    async getMenuList() {
      const { param } = await WhenScreeningList({
        dgwkName: this.$route.query.dgwkName,
      });
      console.log("menu", param.status);
      param.level.forEach((v) => (v.state = false));
      this.lvList = param.level;
      param.status.forEach((v) => (v.state = false));
      this.stateList = param.status;
      this.stateListClone = JSON.parse(JSON.stringify(this.stateList));
    },
    onLoad() {},
    onClickRight() {},
    async onSearch() {
      let query = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        title: this.search,
        dgwkName: this.$route.query.dgwkName,
        level: this.level,
        s_firstDtm_min: this.startTime,
        s_firstDtm_max: this.endTime,
        status: this.stateListId,
        examineStatus: this.stateListId1,
        orgNo: window.localStorage.getItem("orgNo"),
        // dfwStatus:
      };

      // return console.log(query);
      const { value } = await WhenHotList(query);
      console.log("data", value);
      this.list.push(...value);
      this.loading = false;
      if (this.list.length < this.pageSize * this.pageNo) {
        this.finished = true;
      }
      this.pageNo++;
    },
    onSearch1() {
      this.pageNo = 1;
      this.list = [];
      this.onSearch();
    },
    onCancel() {
      this.pageNo = 1;
      this.list = [];
      this.onSearch();
    },
    toJump(item) {
      // return console.log(item);
      this.$router.push({
        path: "JobDetails1",
        query: {
          dgwkNo: item.dgwkNo,
          dgwkName: this.$route.query.dgwkName,
        },
      });
    },
    reset() {
      this.stateList.forEach((v) => {
        v.state = false;
      });
      this.stateList1.forEach((v) => {
        v.state = false;
      });
      this.lvList.forEach((v) => {
        v.state = false;
      });

      this.startTime = "";
      this.endTime = "";
      this.pageNo = 1;
    },
    ok() {
      this.pageNo = 1;
      this.show = false;
      this.list = [];
      this.stateListClone = JSON.parse(JSON.stringify(this.stateList));
      this.stateListClone1 = JSON.parse(JSON.stringify(this.stateList1));
      // let query = {};

      // 开始时间
      // if (this.startTime) {
      //   query.s_firstDtm_min = this.startTime;
      // }
      // // 结束时间
      // if (this.endTime) {
      //   query.s_firstDtm_max = this.endTime;
      // }
      this.onSearch();
    },
    prompt(item) {
      this.postItem = JSON.parse(JSON.stringify(item));
      console.log(this.postItem);
      this.postText = item.oprName;
      // item.oprName == "发起流程" && (this.postText = "发起流程");
      // item.oprName == "布置完成" && (this.postText = "布置完成作业现场");
      // item.oprName == "作业" && (this.postText = "开始作业");
      // item.oprName == "终结" && (this.postText = "完成作业");

      this.show1 = true;
    },
    async confirm() {
      let pic;
      let pic1;
      console.log(this.postText);
      if (this.postText == "现场审核通过") {
        pic = await uploadImages(this.sitePhoto);
        pic = pic.join(",");
      }
      if (this.postText == "验收通过") {
        pic1 = await uploadImages(this.accePic);
        pic1 = pic1.join(",");
        console.log("pic1", pic1);
      }
      console.log(1);
      await InitiateTheProcess({
        dgwkName: this.$route.query.dgwkName,
        ...this.postItem,
        sitePhoto: pic,
        accePic: pic1,
        sign: "update",
        //  dgwkNo:
        // oprTyp:
        // oprldx:
      });
      this.sitePhoto = [];
      this.accePic = [];
      this.show1 = false;
      this.finished = false;
      this.pageNo = 1;
      this.list = [];
      this.onSearch();
    },
    startConfirm(value) {
      console.log(this.getTime(value));
      this.startTime = this.getTime(value);
      this.minDate1 = value;
      this.showTime1 = false;
    },
    endConfirm(value) {
      console.log(this.getTime(value));
      this.endTime = this.getTime(value);
      this.maxDate = value;
      this.showTime2 = false;
    },
    getTime(time) {
      const year = time.getFullYear();
      const Month = time.getMonth() + 1;
      const Date = time.getDate();
      let hour = time.getHours();
      hour = hour < 10 ? "0" + hour : hour;
      let m = time.getMinutes();
      m = m < 10 ? "0" + m : m;
      return `${year}-${Month}-${Date} ${hour}:${m}`;
    },
    changeState(item) {
      item.state = !item.state;
      let arr = [];
      let arr1 = this.stateList.filter((v) => v.state);
      arr1.forEach((v) => {
        v.examineStatus.forEach((v1) => {
          let flag = true;
          arr.forEach((e) => {
            if (e.name == v1.name) {
              flag = false;
            }
          });
          if (flag) {
            arr.push(v1);
          }
        });
      });
      arr.forEach((v) => {
        v.state = false;
      });
      // this.stateList1 = arr; //错误的方案
      this.stateList1 = JSON.parse(JSON.stringify(arr));
    },
    changeState1(item) {
      item.state = !item.state;
    },
    closed() {
      this.stateList = JSON.parse(JSON.stringify(this.stateListClone));
      this.stateList1 = JSON.parse(JSON.stringify(this.stateListClone1));
    },
    async onRefresh() {
      this.pageNo = 1;
      this.finished = false;
      this.list = [];
      await this.onLoad();
      this.isLoading = false;
    },
  },
  components: {
    Tag,
    Dialog,
    Button,
    Search,
    NavBar,
    Icon,
    List,
    Cell,
    Popup,
    DatetimePicker,
    PullRefresh,
    Uploader,
    Field,
  },
};
</script>

<style scoped lang="less">
@import "~@/style/var.less";
* {
  box-sizing: border-box;
}
.WorkRecord {
  .flex {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  background-color: #f7f7f9;
  // /deep/.van-nav-bar__content {
  //   background-color: #3982f6;

  //   .van-nav-bar__title {
  //     color: #fff;
  //   }
  // }
  .placeholder {
    width: 5px;
    height: 30px;
    background: @primary-linear;
    margin: 0 16px;
  }
  .item {
    border-top: 1px solid #eee;
    margin-top: 20px;
    background-color: #fff;
    .row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 72px;
      border-bottom: 1px solid #dddddd;

      .title {
        font-size: 32px;
        font-weight: 700;
        margin-left: 37px;
        .id {
          margin-left: 25px;
          font-size: 28px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #666666;
        }
        .ico {
          margin-left: 5px;
          font-size: 30px;
          color: #a7a7a7;
          font-weight: 100;
        }
      }
      .state {
        margin-right: 20px;
        // width: 132px;
        height: 44px;
        border-radius: 22px;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 28px;
        color: @primary;
        font-family: PingFang SC;
        font-weight: 500;
      }
    }
    .row1 {
      justify-content: start;
      font-size: 28px;
      color: #999999;
      border: 0;
      // margin-left: 36px;
      .type {
        width: 100%;
        height: 100%;
        border-bottom: 1px solid #dddddd;
        display: flex;
        align-items: center;
        font-size: 28px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #696d73;
      }
    }

    .row3 {
      width: 100%;
      height: 72px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      padding-right: 40px;
      .btn {
        width: 132px;
        height: 44px;
        margin-right: 20px;
        // background: @primary;
        border: 1px solid @primary;
        border-radius: 22px;
        font-size: 28px;
        font-family: PingFang SC;
        font-weight: 500;
        color: @primary;
        padding: 10px 35px;
      }
      .btn1 {
        background: #f8e0ce;
        color: @primary;
        border: 0;
      }
    }
    .main {
      position: relative;
      padding: 5px 38px;
      .row-item {
        display: flex;
        align-items: center;
        margin-top: 15px;
        .attr {
          width: 200px;
          font-size: 28px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #999999;
        }
        .val {
          font-size: 28px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #333333;
        }
      }
      img {
        width: 120px;
        height: 120px;
        position: absolute;
        right: 23px;
        top: 23px;
      }
    }
  }
  .prompt {
    display: flex;
    flex-flow: column;
    border-radius: 10px;
    .title {
      height: 80px;
      font-size: 30px;
      font-family: PingFang SC;
      font-weight: bold;
      color: #fff;
      border-bottom: 2px solid #eee;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: @primary;
    }
    .main {
      flex: 1;
      display: flex;
      flex-flow: column;
      justify-content: space-around;
      align-items: center;
      font-size: 28px;
      color: #989898;
      .message {
        display: flex;
        justify-content: center;
        // align-items: center;
        .tag {
          margin-right: 10px;
        }
      }
      .btns {
        width: 100%;
        margin-top: 50px;
        display: flex;
        justify-content: center;
        .btn {
          width: 130px;
          height: 55px;
          border-radius: 5px;
          // margin-left: 50px;
          margin: 0 30px;
        }
      }
    }
  }
  .prompt2 {
    box-sizing: border-box;
    // overflow: hidden;
    .title {
      height: 80px;
      font-size: 30px;

      font-family: PingFang SC;
      font-weight: bold;
      color: #666666;
      border-bottom: 2px solid #eee;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .main2 {
      padding: 0 37px;
      .title2 {
        margin-top: 26px;
        font-size: 26px;
      }
      .pwd {
        position: relative;
        padding: 5px;
        font-size: 24px;
        width: 100%;
        height: 50px;
        margin-top: 26px;
        border: 1px solid #d7d7d7;
        img {
          height: 80%;
          position: absolute;
          right: 10px;
        }
      }
      textarea {
        padding: 5px;
        font-size: 24px;
        margin-top: 26px;
        width: 100%;
        height: 210px;
        border: 1px solid #d7d7d7;
        border-radius: 6px;
      }
    }
    .btns {
      width: 100%;
      margin-top: 50px;
      display: flex;
      justify-content: center;
      .btn {
        width: 130px;
        height: 55px;
        border-radius: 5px;
        margin: 0 30px;
      }
    }
  }
  .Popup {
    .task {
      border-bottom: 1px solid #a7a7a7;
    }
    .title {
      padding: 15px;
      font-size: 28px;
      color: #a7a7a7;
    }
    .state {
      display: flex;
      //   justify-content: space-between;
      flex-direction: row;
      font-size: 26px;
      color: #6e6e6e;
      flex-wrap: wrap;
      .state-item {
        width: 33%;
        height: 74px;
        .btn3 {
          position: relative;
          width: 157px;
          height: 58px;
          background-color: #f6f6f6;
          border-radius: 4px;
          color: #6e6e6e;
          &.change {
            border: 3px solid @primary;
            background: #fff;
          }
          &.change::after {
            content: "\F0C8";
            height: 30px;
            font-family: "vant-icon";
            text-indent: 16px;
            line-height: 1.7;
            background-repeat: no-repeat;
            position: absolute;
            bottom: -2px;
            right: -2px;
            color: #fff;
            background-image: linear-gradient(
              146deg,
              transparent 0%,
              transparent 50%,
              @primary 50%,
              @primary 100%
            );
          }
        }
      }
    }
    .ipt {
      width: 90%;
      height: 58px;
      text-indent: 1em;
      font-size: 24px;
      margin-left: 20px;
      background-color: #f6f6f6;
    }
    .ipt1 {
      width: 40%;
      height: 58px;
      text-indent: 1em;
      font-size: 24px;

      background-color: #f6f6f6;
    }
    .placeholder1 {
      width: 60px;
      height: 58px;
      color: #3982f6;
    }
    .btn-box {
      position: absolute;
      bottom: 0;
      width: 100%;
      margin-bottom: 40px;
      .ok {
        width: 157px;
        height: 58px;
        border: 1px solid @primary;
        border-radius: 4px;
        color: @primary;
        margin-left: 50px;
      }
      .reset {
        width: 157px;
        height: 58px;
        // background-color: #dddddd;
        border-radius: 4px;
        color: #6e6e6e;
      }
    }
  }
  /deep/.van-uploader__upload {
    border: 2px dashed #e16712;
    width: 110px;
    height: 110px;
    border-radius: 4px;
    background: #fff;
    // margin-left: 30px;
    .van-icon {
      color: rgb(225, 103, 18);
      &::before {
        content: "\F0A2";
      }
    }
  }
  .uploaderList {
    padding-left: 30px;
  }
}
</style>
